package org.skyeye.modeling.images;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.skyeye.modeling.config.AppConstant;
import org.skyeye.modeling.model.MBase;
import org.skyeye.modeling.model.MDevice;
import org.skyeye.modeling.model.MRoot;
import org.skyeye.modeling.model.MSOC;
import org.skyeye.modeling.ui.PracticeEditor;

public class ImageConfig {
	// test.conf
	private String configFileName;
	private boolean isEnabled;
	private HashMap<String, Map<String, List<String>>> socOutFileMapping = new HashMap();

	public String getConfigFileName() {
		return configFileName;
	}

	public void setConfigFileName(String configFileName) {
		this.configFileName = configFileName;
	}

	public boolean isEnabled() {
		return isEnabled;
	}

	public void setEnabled(boolean isEnabled) {
		this.isEnabled = isEnabled;
	}

	public HashMap<String, Map<String, List<String>>> getSocOutFileMapping() {
		return socOutFileMapping;
	}

	public void setSocOutFileMapping(HashMap<String, Map<String, List<String>>> socOutFileMapping) {
		this.socOutFileMapping = socOutFileMapping;
	}

	public Map<String, List<String>> getOutFile(String socName) {
		return this.socOutFileMapping.get(socName);
	}

	public void setSocOutFileMapping(String socName, Map<String, List<String>> outFileName) {
		this.socOutFileMapping.put(socName, outFileName);
	}

	public String toString() {
		StringBuffer result = new StringBuffer();
		for (Entry<String, Map<String, List<String>>> soc : socOutFileMapping.entrySet()) {
			String socName = soc.getKey();
			if(socName.startsWith("lf")){
				Map<String, List<String>> cores = soc.getValue();
				for (Entry<String, List<String>> ss : cores.entrySet()) {
					String cpuName = ss.getKey(); 
					List<String> pathandAddr = ss.getValue();
					for(String p : pathandAddr){
						result.append("load-file ").append(cpuName).append("  ").append(p).append("\n");
					}
				}
			}else{
				Map<String, List<String>> cores = soc.getValue();
				for (Entry<String, List<String>> ss : cores.entrySet()) {
					String cpuName = ss.getKey(); 
					List<String> path = ss.getValue();
					for(String p : path){
						result.append("load-binary ").append(cpuName).append("  ").append(p).append("\n");
					}
				}
			}
		}
		return result.toString();
	}

	@SuppressWarnings("unused")
	private List<String> getCPUNameListBySocName(String socName) {
		List<String> cpuNames = new ArrayList<String>();

		MRoot root = PracticeEditor.mroot;
		if (root != null) {
			for (MBase node : root.getChildren()) {
				if (node instanceof MSOC) {
					if (((MSOC) node).getName().equals(socName)) {

						for (MBase n : node.getChildren()) {
							if (n instanceof MDevice && ((MDevice) n).getBaseType().equals(AppConstant.BASE_CPU)) {
								cpuNames.add(((MDevice) n).getName());
							}
						}

					}

				}
			}
		}
		return cpuNames;
	}
}
